// pages/sellWater/sellWater.js
var redg = -1080;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr: [
      // { id:'01', name: '刷卡预扣费', num: '1', unit: '分', animationData:{}},
      // { id:'02', name: '脉冲数', num: '2', unit: '个', animationData: {}},
      // { id:'03', name: '预扣费取水时长', num: '3', unit: '秒', animationData: {} },
      // { id:'04', name: '投币器个数', num: '4', unit: '个', animationData: {}},
      // { id:'05', name: '预扣费取水量', num: '5', unit: '毫升', animationData: {}},
      // { id:'06', name: '每秒显示出水量', num: '10', unit: '毫升', animationData: {}},
      // { id:'07', name: '金额回收时间', num: '6', unit: '秒', animationData: {}},
      // { id:'08', name: '打水灯延迟关闭时间', num: '0', unit: '秒', animationData: {}},
      // { id:'09', name: '取水门锁延时关闭时间', num: '20', unit: '秒', animationData: {}}
    ],
    value_a: 0,
    sn: '',
    originData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      sn: options.eq_sn
      // sn: 'f56a0130bb8ee5'
    });
    that.getSaleWaterParam();
  },


  /**
   * 获取售水参数
   */
  getSaleWaterParam() {
    var that = this;
    app.ajax({
      url: 'Manage/Equipment/getSaleWaterParam',
      method: "POST",
      data: {
        sn: that.data.sn
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var tmpArr = [];
          var data = res.data.data;
          for (var i = 0; i < data.length; i++) {
            var tmp = {};
            tmp.name = data[i]['name'];
            tmp.num = data[i]['value'];
            tmp.unit = data[i]['unit'];
            tmp.id = data[i]['id'];
            tmp.animationData = {};
            tmpArr.push(tmp);
          }
          that.setData({
            listArr: tmpArr,
            originData: JSON.parse(JSON.stringify(tmpArr))
          });

        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },


  //增加
  add: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var nowNum = newArr[indnum].num;
    if (nowNum < 9999999) {
      newArr[indnum].num++
    }
    //console.log("修改之后的数组", newArr);
    that.setData({
      listArr: newArr
    })
  },
  //减少
  cut: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var nowNum = newArr[indnum].num;
    if (nowNum > 0) {
      newArr[indnum].num--
    }
    //console.log("修改之后的数组", newArr);
    that.setData({
      listArr: newArr
    })
  },
  //input输入
  inputChnge: function (e) {
    var that = this;
    var inpval = e.detail.value;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    if (inpval >= 0) {
      newArr[indnum].num = inpval;
      that.setData({
        listArr: newArr
      })
    }
  },
  //刷新
  updata: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    })
    if (redg) {
      redg -= 1080;
    }
    that.animation = animation
    animation.rotate(redg).step()
    newArr[indnum].animationData = animation //控制对应的动画
    that.setData({
      listArr: newArr
    });
    //请求接口
    app.ajax({
      url: 'Manage/Equipment/saleWaterParamEdit',
      method: "POST",
      data: {
        sn: that.data.sn,
        param: newArr[indnum].id,
        value: newArr[indnum].num
      },
      success: function (res) {
        if (res.data.code == 1000) {
          const originData = that.data.originData;
          originData[indnum].num = newArr[indnum].num;
          setTimeout(() => {
            that.setData({
              originData
            })
          }, 2000)
          app.showToast(res.data.message, "none", 2000, function () { });
        } else if (res.data.code == -1000) {
          app.showToast(res.data.message, "none", 2000, function () { });
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },
})